﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LINQPad.Extensibility.DataContext;
using System.IO;

namespace LinqPadSP2010ContextDriver
{
    public partial class ConnectionDialogGDI : Form
    {
        IConnectionInfo _cxInfo;
        
        public ConnectionDialogGDI(IConnectionInfo cxInfo)
        {
            _cxInfo = cxInfo;
			InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Uri u;
            if (!Uri.TryCreate(this.txtSharePointUrl.Text, UriKind.Absolute, out u))
            {
                MessageBox.Show(string.Format("The Uri {0} is not valid", this.txtSharePointUrl.Text));
                return;
            }


            this._cxInfo.DriverData.SetElementValue("SPUrl", u.AbsoluteUri);
            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();

        }

        private void lnkBrowseAssembly_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog()
            {
                Title = "Choose custom assembly",
                DefaultExt = ".dll",
            };

            if (dialog.ShowDialog() == true)
            {
                _cxInfo.CustomTypeInfo.CustomAssemblyPath = dialog.FileName;
            }
        }

        private void lnkChooseType_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string assemPath = _cxInfo.CustomTypeInfo.CustomAssemblyPath;
            if (assemPath.Length == 0)
            {
                MessageBox.Show("First enter a path to an assembly.");
                return;
            }

            if (!File.Exists(assemPath))
            {
                MessageBox.Show("File '" + assemPath + "' does not exist.");
                return;
            }

            string[] customTypes;
            try
            {
                // TODO: In a real-world driver, call the method accepting a base type instead (unless you're.
                // working with a POCO ORM). For instance: GetCustomTypesInAssembly ("System.Data.Linq.DataContext")
                // You can put interfaces in here, too.
                customTypes = _cxInfo.CustomTypeInfo.GetCustomTypesInAssembly("Microsoft.SharePoint.Linq.DataContext");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error obtaining custom types: " + ex.Message);
                return;
            }
            if (customTypes.Length == 0)
            {
                MessageBox.Show("There are no public types in that assembly.");  // based on.........
                return;
            }

            string result = (string)LINQPad.Extensibility.DataContext.UI.Dialogs.PickFromList("Choose SharePoint Context", customTypes);
            if (result != null) _cxInfo.CustomTypeInfo.CustomTypeName = result;
        }

        

       
        

		

        
    }
}
